package com.lianbang.pharmacy.vm

import androidx.lifecycle.MutableLiveData
import cn.ubest.mvvmcore.base.BaseListViewModel
import cn.ubest.mvvmcore.interfaces.RequestHandler1Impl
import com.lianbang.pharmacy.bean.MedicineInfoBean
import com.lianbang.pharmacy.bean.MedicineSupplierListBean
import com.lianbang.pharmacy.bean.SupplierListBean
import com.lianbang.pharmacy.model.MedicineModel

/**
 * Created by maoqi on 2020/12/29.
 */
class SupplierVM : BaseListViewModel<MedicineModel>() {
    val mMedicineLiveData by lazy {
        MutableLiveData<MedicineInfoBean>()
    }
    val mSupplierListLiveData by lazy {
        MutableLiveData<List<MedicineSupplierListBean>>()
    }

    override fun loadData(isInit: Boolean, vararg arg: Any) {
        requestWithLife(object : RequestHandler1Impl<List<SupplierListBean>>(this) {
            override suspend fun request1(): List<SupplierListBean> {
                return mModel.supplierList().resolveWrapper()
            }

            override fun onSuccess(t: List<SupplierListBean>) {
                setData(t)
            }

        }, isInit)
    }

    fun medicineInfo(id: Int) {
        requestWithLife(object : RequestHandler1Impl<MedicineInfoBean>(this) {
            override suspend fun request1(): MedicineInfoBean {
                return mModel.medicineInfo(id).resolveWrapper()
            }

            override fun onSuccess(t: MedicineInfoBean) {
                mMedicineLiveData.postValue(t)
            }

        })
    }

    fun medicineSupplierList(id: Int) {
        requestWithLife(object : RequestHandler1Impl<List<MedicineSupplierListBean>>(this) {
            override suspend fun request1(): List<MedicineSupplierListBean> {
                return mModel.medicineSupplierList(id).resolveWrapper()
            }

            override fun onSuccess(t: List<MedicineSupplierListBean>) {
                mSupplierListLiveData.postValue(t)
            }

        })
    }
}